
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include "header.php";
$page = "transaction";

//check user login
if (!$user->user_exists) {
    header('Location: ./login.php');
    exit();
}
$id = null;

$errors = array();
$search = false;
$class = new Transaction();
$submit = 0;
$transaction = array();
$step = 1;

if (isset($_POST['id'])) {
    $id = $_POST['id'];
} elseif (isset($_GET['id'])) {
    $id = $_GET['id'];
}
if (isset($_POST['step'])) {
    $step = $_POST['step'];
} elseif (isset($_GET['step'])) {
    $step = $_GET['step'];
}

if(!empty($id)){
    $transaction = $class->getTransactions($id);
}
if(empty($transaction)){
    $errors[] = 'No data found';
}

$code = @$transaction['contract_code'];
$contract = $class->getContract($code);

if (isset($_POST['content_id'])) {
    $content_id = $_POST['content_id'];
} elseif (isset($_GET['content_id'])) {
    $content_id = $_GET['content_id'];
}else{
    $content_id = null;
}

if (isset($_POST['repair_id'])) {
    $repair_id = $_POST['repair_id'];
} elseif (isset($_GET['repair_id'])) {
    $repair_id = $_GET['repair_id'];
}else{
    $repair_id = null;
}


if(isset($_POST['submit']) && !empty($_POST['submit'])){
    if(!$class->editTransaction($id,$_POST)){
        $errors = 'Canot Update!!';
    }elseif(!empty($_POST['submit'])){
        $step ++;
    }
}
$smarty->assign('transaction', $transaction);
$smarty->assign("repair_users", $class->getRepairUsers());
$smarty->assign("key_users", $class->getKeyUsers());
$smarty->assign("contents", $class->getContents());
$smarty->assign('content_id', $content_id);
$smarty->assign('repair_id', $repair_id);
$smarty->assign('contract', $contract);
$smarty->assign('search', $search);
$smarty->assign('step', $step);

$smarty->assign('errors', $errors);

include "footer.php";